<?php
class koudai_productControl extends skymvc{
	public $typeid; 
	public function __construct(){
		parent::__construct();
		$this->typeid=2; 	 
	}
	
	function onDefault(){
		
		//M("koudai_product")->update(array("status"=>2)," shopid=".SHOPID." AND status=4 AND total_num > 0 ");
		 
		$status=get('status','i');
		$status=$status?$status:2; 
		
		$limit=100;
		$where=" status=$status AND shopid=".SHOPID;
		$url=APPKOUDAI."?m=koudai_product&a=my&status=".$status;
		if($status==2){
			if(get('empty')){
				$where.=" AND total_num=0 ";
				$url.="&empty=1";
			}else{
				$where.=" AND total_num>0 ";
			}
		}
		
		$keyword=get('keyword','h'); 
		if($keyword){
			$where="  status<10 AND shopid=".SHOPID;
			$proids=M("product")->selectCols(array(
				"where"=>" title like '%".$keyword."%' ",
				"fields"=>"id"
			));
			if($proids){
				$where.=" AND productid in("._implode($proids).")";
			}else{
				$where.=" AND 1=2 ";
			}
		} 
		
		$start=get('per_page','i');
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("koudai_product")->select($option,$rscount);
		if($data){
				foreach($data as $k=>$v){
					$productids[]=$v['productid'];
					$shopids[]=$v['shopid'];
				}
				$products=M('product')->getListByIds($productids);
			 	$shops=M('koudai')->getShopByIds($shopids);
				foreach($data as $k=>$v){
					$v['title']=$products[$v['productid']]['title'];
					$v['price']=$products[$v['productid']]['price'];
					$v['lower_price']=$products[$v['productid']]['lower_price'];
					$v['imgurl']=$products[$v['productid']]['imgurl'];
					$v['shopname']=$shops[$v['shopid']]['title'];
					$data[$k]=$v;
				}
			}
	 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(
			array(
				"data"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				 
			)
		);
		$this->smarty->display("koudai_product/index.html");
	}
	
	
	public function onTBKC(){
		return false;
		/*
		**口袋同步库存处理
		*/
		$key="a_koudai_tbkc";
		$cache=Cache()->get($key);
		if(!$cache){
			$koudai=M("koudai")->selectRow("id=".SHOPID);
			$kdkc=M("kucun")->selectRow("koudai=".SHOPID);
			if(empty($kdkc)){
				$site=M("sites")->selectRow("siteid=".$koudai['siteid']);
				$kcid=M("kucun")->insert(array(
					"title"=>$site['sitename'],
					"koudai"=>SHOPID,
					"typeid"=>2,
					"status"=>2				 
				));
				M("kucun_area")->insert(array(
					"siteid"=>$koudai['siteid'],
					"kcid"=>$kcid
				));										
			}else{
				$kcid=$kdkc['kcid'];
				
			}
			/*同步库存产品*/
			$kdlist=M("koudai_product")->select(array(
				"where"=>" shopid=".SHOPID,
			));
			
			if($kdlist){
				foreach($kdlist as $v){
					$row=M("kucun_product")->selectRow("shopid=".$kcid." AND productid=".$v['productid']);
					if($row){
						if($row['total_num']!=$v['total_num']){
							M("kucun_product")->update(array(
								"total_num"=>$v['total_num'],
								"jh_price"=>$v['price'],
							),"id=".$row['id']);
						}
					}else{
						M("kucun_product")->insert(array(
							"shopid"=>$kcid,
							"productid"=>$v['productid'],
							"total_num"=>$v['total_num'],
							"status"=>$v['status'],
							"jh_price"=>$v['price'],
						));
					}
				}
			}
			cache()->set($key,1,30);
			echo "同步成功";
		}else{
			echo "同步失败";
		}
		/*End同步库存产品*/
	}
	
	/*发表产品模型*/
	public function onAdd(){
		
		$kcids=M("kucun_area")->selectCols(array(
			"where"=>"  kcid>0 AND  siteid=".SITEID,
			"fields"=>"kcid"
		));
		
		if(empty($kcids)){
			$this->goALl("您所在区域暂无库存哦");
		}
		$kucunlist=M("kucun")->getListByIds($kcids);
 
		$where=" 1 ";
		$url=APPKOUDAI."?m=mdian_product&a=add";
		$kcid=get('kcid','i');
		if($kucunlist){
			foreach($kucunlist as $k=>$v){
				if($v['koudai']){
					unset($kucunlist[$k]);
				}else{
					$kkcids[]=$v['kcid'];
				}
			}
		}
	 	
		$kcid=get('kcid','i');
		if(!$kcid){
			$kcid=$kkcids[0];
		}
		if(!$kcid){
			$this->goAll("暂时没有商品库存");
		}
		$keyword=get('keyword','h');
		if($keyword){
			$where.=" AND title like '%".$keyword."%' ";
		}
		$kcps=M("kucun_product")->select(array(
			"where"=>" shopid=".$kcid." AND status=2 ",
		));
		if($kcps){
			foreach($kcps as $v){
				$productids[]=$v['productid'];
				$kcproducts[$v['productid']]=$v;
			}
		}
		if($productids){
			$where.=" AND    id in("._implode($productids).")";
		}else{
			$where.=" AND 1=2 ";
		}
		$start=get('per_page','i'); 
		$limit=2000;
		$option=array(
			"where"=>$where,
			"order"=>"buy_num DESC",
			"limit"=>$limit,
			"start"=>$start
		);
		$rscount=true;
		$kclist=M("product")->select($option,$rscount);
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$ymproducts=M("koudai_product")->GetProductIdsBySHop(array("where"=>"shopid=".SHOPID." AND status in(2,4) "));
		if($kclist){
			foreach($kclist as $k=>$v){
				$v['total_num']=$kcproducts[$v['id']]['total_num'];
				$v['kcid']=$kcproducts[$v['id']]['shopid'];
				if(isset($ymproducts[$v['id']])){
					$v['ym_total_num']=$ymproducts[$v['id']]['total_num'];
					$v['ym_buy_num']=$ymproducts[$v['id']]['buy_num'];
					$v['ym_catid']=$ymproducts[$v['id']]['catid'];
					$v['koudai']=1;
					
				}			 
				$kclist[$k]=$v;
			}
		}
		
		$this->smarty->assign(array(
			"kclist"=>$kclist,
			"pagelist"=>$pagelist,
			"kcid"=>$kcid,
			"kucunlist"=>$kucunlist,
		));
	 
		$this->smarty->display("koudai_product/add.html");
	}
	
	public function onSave(){
		 
	}
	
	public function onAddProduct(){
			 
			$productid=get_post('productid','i');
			$kcid=get_post('kcid','i'); 
			$kucun_product=M('kucun_product')->selectRow("productid=".$productid." AND shopid=".$kcid);
			$product=M("product")->selectRow("id=".$productid);
			if(empty($kucun_product)){
				$this->goAll("库存产品不存在");
			}
			$catid=get_post('catid','i');
			$row=M('koudai_product')->selectRow("shopid=".SHOPID." AND productid=".$productid." ");
			$total_num=get_post('total_num','i');
			$catid=get_post('catid','i');
			/*判断库存产品是否够了*/
			 
			if($row){
				$id=$row['id'];
				if($row['shopid']!=SHOPID){
					$this->goAll("你无权限",2);
				}
				M("koudai_product")->update(array(
					"catid"=>$catid,
					"status"=>2
				),"id=".$id);
				
			}else{
				
				$data=array(
					"productid"=>$productid,
					"shopid"=>SHOPID,
					"dateline"=>time(),
					"catid"=>$catid,
					"status"=>2,
					"total_num"=>0,
					"siteid"=>SITEID,
					"price"=>$product['kd_price']
				);
				$id=M("koudai_product")->add($data);
			}	
			
			$this->goall("保存成功");
	}
	
	public function onAddKucun(){
		$id=get("id",'i');
		
		$row=M("koudai_product")->selectRow("id=".$id);
		
		if(empty($row)){
			$this->goAll("数据出错");
		}
		$kcids=M("kucun_area")->selectCols(array(
			"where"=>" siteid=".SITEID,
			"fields"=>"kcid"
		));
		
		if(empty($kcids)){
			$this->goALl("您所在区域暂无库存哦");
		}
		
		$kcids=M("kucun_product")->selectCols(array(
			"where"=>" productid=".$row['productid']." AND shopid in("._implode($kcids).")",
			"fields"=>"shopid"
		));
		 
		if($kcids){
			$kclist=M("kucun")->getListByIds($kcids);
		}
		$this->smarty->assign(
			array(
				"product"=>$row,
				"kclist"=>$kclist
			)
		);
		echo $data=$this->smarty->fetch("koudai_product/addkucun.html");
	}
	
	public function onaddkucunSave(){
		$id=get("id",'i');
		$kcid=get_post('kcid','i'); 
		$row=M("koudai_product")->selectRow("id=".$id);
		if(empty($row) or $row['shopid']!=SHOPID){
			$this->goAll("数据出错");
		}
		$total_num=get_post("num",'i');
		$kucun_product=M("kucun_product")->selectRow("productid=".$row['productid']." AND shopid=".$kcid);
		 
		$product=M("product")->selectRow("id=".$row['productid']);
		if($total_num>$kucun_product['total_num']){
			$this->goAll("库存不足",1);
		}
		 
		//发布库存到购物车
		
		$cart=M("kcorder_cart")->selectRow("shopid=".SHOPID." AND typeid=".$this->typeid." AND productid=".$row['productid']." AND object_id=".$id." AND kcid=".$kcid."");
			if($cart){
				M("kcorder_cart")->update(
					array(
						"amount"=>$cart['amount']+$total_num
					),"id=".$cart['id']
				);
			}else{
				M("kcorder_cart")->insert(array(
					"productid"=>$row['productid'],
					"object_id"=>$id,
					"dateline"=>time(),
					"amount"=>$total_num,
					"shopid"=>SHOPID,
					"siteid"=>SITEID,
					"typeid"=>$this->typeid,
					"kcid"=>$kcid,
					"price"=>$product['price']
				));	
			}
		$this->goall("库存添加成功");
	}
	
	public function onOrder(){
		$kcids=M("kucun_area")->selectCols(array(
			"where"=>"  kcid>0 AND siteid=".SITEID,
			"fields"=>"kcid"
		));
		
		if(empty($kcids)){
			$this->goALl("您所在区域暂无库存哦");
		}
		$kucunlist=M("kucun")->getListByIds($kcids,2);
		if($kucunlist){
			foreach($kucunlist as $k=>$v){
				if($v['koudai']){
					unset($kucunlist[$k]);
				}else{
					$kkcids[]=$v['kcid'];
				}
			}
		}
	 	
		$kcid=get('kcid','i');
		if(!$kcid){
			$kcid=$kkcids[0];
		}
	 
		if(!$kcid){
			$this->goAll("暂时没有商品库存");
		}
		$kcproducts=M("kucun_product")->GetProductIdsBySHop(array(
			"where"=>" shopid=".$kcid." AND total_num>0 AND status=2  ",
			"fields"=>"productid,shopid,total_num"
		));
		 
		if($kcproducts){
			foreach($kcproducts as $v){
				$productids[]=$v['productid'];
			}
		}
		$limit=2000;
		$where=" status in(2,4) AND shopid=".SHOPID;
		if($productids){
			$where.=" AND productid in("._implode($productids).")";
		}else{
			$where.=" AND 1=2 ";
		}
		$url=APPKOUDAI."?m=koudai_product&a=order";
		$start=get('per_page','i');
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"total_num ASC,id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("koudai_product")->select($option,$rscount);
		if($data){
				foreach($data as $k=>$v){
					$productids[]=$v['productid'];
					$shopids[]=$v['shopid'];
				}
				$products=M('product')->getListByIds($productids);
			 	$shops=M('koudai')->getShopByIds($shopids);
				foreach($data as $k=>$v){
					$v['kc_total_num']=$kcproducts[$v['productid']]['total_num'];
					$v['title']=$products[$v['productid']]['title'];
					$v['price']=$products[$v['productid']]['price'];
					$v['lower_price']=$products[$v['productid']]['lower_price'];
					$v['imgurl']=$products[$v['productid']]['imgurl'];
					$v['shopname']=$shops[$v['shopid']]['title'];
					$v['jh_notenum']=$products[$v['productid']]['jh_notenum'];
					$v['kclist']=$this->kclist($v['id']);
					$data[$k]=$v;
				}
			}
	 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(
			array(
				"data"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				 "kucunlist"=>$kucunlist,
				 "kcid"=>$kcid
			)
		);
		$this->smarty->display("koudai_product/order.html");
	}
	
	public function onBack(){
		$kcids=M("kucun_area")->selectCols(array(
			"where"=>"  kcid>0 AND siteid=".SITEID,
			"fields"=>"kcid"
		));
		
		if(empty($kcids)){
			$this->goALl("您所在区域暂无库存哦");
		}
		$kucunlist=M("kucun")->getListByIds($kcids);
		if($kucunlist){
			foreach($kucunlist as $k=>$v){
				if($v['koudai']){
					unset($kucunlist[$k]);
				}else{
					$kkcids[]=$v['kcid'];
				}
			}
		}
	 	
		$kcid=get('kcid','i');
		if(!$kcid){
			$kcid=$kkcids[0];
		}
		
		if(!$kcid){
			$this->goAll("暂时没有商品库存");
		}
		$productids=M("kucun_product")->selectCols(array(
			"where"=>" shopid=".$kcid,
			"fields"=>"productid"
		));
		$status=get('status','i');
		$status=$status?$status:2; 
		$limit=2000;
		$where=" status=$status AND shopid=".SHOPID;
		if($productids){
			$where.=" AND productid in("._implode($productids).")";
		}else{
			$where.=" AND 1=2 ";
		}
		$url=APPKOUDAI."?m=koudai_product&a=back";
		$start=get('per_page','i');
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"total_num DESC,id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("koudai_product")->select($option,$rscount);
		if($data){
				foreach($data as $k=>$v){
					$productids[]=$v['productid'];
					$shopids[]=$v['shopid'];
				}
				$products=M('product')->getListByIds($productids);
			 	$shops=M('koudai')->getShopByIds($shopids);
				foreach($data as $k=>$v){
					$v['title']=$products[$v['productid']]['title'];
					$v['price']=$products[$v['productid']]['price'];
					$v['lower_price']=$products[$v['productid']]['lower_price'];
					$v['imgurl']=$products[$v['productid']]['imgurl'];
					$v['shopname']=$shops[$v['shopid']]['title'];
					$v['jh_notenum']=$products[$v['productid']]['jh_notenum'];
					$v['kclist']=$this->kclist($v['id']);
					$data[$k]=$v;
				}
			}
	 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(
			array(
				"data"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				 "kucunlist"=>$kucunlist,
				 "kcid"=>$kcid
			)
		);
		$this->smarty->display("koudai_product/back.html");
	}
	
	public function kclist($id){
		$row=M("koudai_product")->selectRow("id=".$id);
		if(empty($row)){
			return false;
		}
		$kcids=M("kucun_area")->selectCols(array(
			"where"=>" siteid=".SITEID,
			"fields"=>"kcid"
		));
		if(empty($kcids)){
			return false;
		}
		$kcids=M("kucun_product")->selectCols(array(
			"where"=>" productid=".$row['productid']." AND shopid in("._implode($kcids).")",
			"fields"=>"shopid"
		));
		if($kcids){
			return $kclist=M("kucun")->getListByIds($kcids);
		}
		
		
		
	}
	
	public function onCutKucun(){
		$id=get("id",'i');
		
		$row=M("koudai_product")->selectRow("id=".$id);
		if(empty($row)){
			$this->goAll("数据出错");
		}
		$kcids=M("kucun_area")->selectCols(array(
			"where"=>" siteid=".SITEID,
			"fields"=>"kcid"
		));
		
		if(empty($kcids)){
			$this->goALl("您所在区域暂无库存哦");
		}
		
		$kcids=M("kucun_product")->selectCols(array(
			"where"=>" productid=".$row['productid']." AND shopid in("._implode($kcids).")",
			"fields"=>"shopid"
		));
		if($kcids){
			$kclist=M("kucun")->getListByIds($kcids);
		}
		$this->smarty->assign(
			array(
				"product"=>$row,
				"kclist"=>$kclist
			)
		);
		echo $data=$this->smarty->fetch("koudai_product/cutkucun.html");
	}
	
	public function oncutkucunSave(){
		$id=get("id",'i');
		$kcid=get_post('kcid','i'); 
		$row=M("koudai_product")->selectRow("id=".$id);
		if(empty($row) or $row['shopid']!=SHOPID){
			$this->goAll("数据出错");
		}
		$product=M("product")->selectRow("id=".$row['productid']);
		$kucun_product=M("kucun_product")->selectRow("productid=".$row['productid']." AND shopid=".$kcid);
		$total_num=get_post("num",'i');
		 
		if($total_num>$row['total_num']){
			$this->goAll("库存不足",1);
		}
		 
		//发布库存到购物车
		$udata=array("total_num"=>$row['total_num']-$total_num);
		if($total_num==$row['total_num']){
			$udata['status']=4;
		}
		M("koudai_product")->update($udata,"id=".$row['id']);
		$cart=M("kcback_cart")->selectRow("shopid=".SHOPID." AND typeid=".$this->typeid." AND productid=".$row['productid']." AND object_id=".$id." AND kcid=".$kcid."");
			if($cart){
				M("kcback_cart")->update(
					array(
						"amount"=>$cart['amount']+$total_num
					),"id=".$cart['id']
				);
			}else{
				M("kcback_cart")->insert(array(
					"productid"=>$row['productid'],
					"object_id"=>$id,
					"dateline"=>time(),
					"amount"=>$total_num,
					"shopid"=>SHOPID,
					"siteid"=>SITEID,
					"typeid"=>$this->typeid,
					"kcid"=>$kcid,
					"price"=>$product['price']
				));	
			}
		$this->goall("库存减少成功",0,$row['total_num']-$total_num);
	}
	
	
	 
	
	public function onForbid(){
		$id=get('id','i');
		$row=M("koudai_product")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		/*if($row['total_num']!=0){
			$this->goAll("暂无权限下架",1);
		}
		*/
		M("koudai_product")->update(array("status"=>4),"id=".$id);
		$this->goall("下线成功");
	}
	
	public function onPass(){
		$id=get('id','i');
		$row=M("koudai_product")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("koudai_product")->update(array("status"=>2),"id=".$id);
		$this->goall("下线成功");
	}
	
	
	public function onDelete(){
		$id=get('id','i');
		$row=M("koudai_product")->selectRow("id=".$id);
		if($row['status']!=4 or  $row['total_num']!=0){
			$this->goAll("暂无权限删除",1);
		}
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("koudai_product")->update(array("status"=>99),"id=".$id);
		$this->goall("删除成功");
	}
	 
	
	 
	
}
?>